/**
 *                     Remixvym (version 0.8.3)
 *             Copyright (c) 2010 by RemixLab, DISI-UNAL
 *            http://www.disi.unal.edu.co/grupos/remixlab/
 *
 * This Java package provides classes to visualize Mind Maps created in VYM
 * (View Your Mind) in a Processing Sketch
 *
 * @author Alvaro Lopez, Juan Baquero
 *
 * This source file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * A copy of the GNU General Public License is available on the World Wide Web
 * at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by
 * writing to the Free Software Foundation, 51 Franklin Street, Fifth Floor
 * Boston, MA 02110, USA
 */
package remixlab.remixvym.core.graphics.node;

import java.util.ArrayList;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import processing.core.PGraphics;
import remixlab.remixvym.core.graphics.StyleGraph;
import remixlab.remixvym.core.graphics.text.TextUtil;
import remixlab.remixvym.core.util.MindMapUtil;
import remixlab.remixvym.core.mindmap.Node;

	
/**
 * This class draws the node: text, background, icons, emoticons, and so on.
 * The border is drawn for one of it subclasses. See {@link NoneNode}, {@link OvalNode} and {@link RectNode}
 * <p>
 * Extends {@link remixlab.remixvym.core.graphics.node.NodeShape}<p>
 * <b>See examples: </b> NodeStyle
 */
public class NodeShape implements ConstansColor,NodeConstans {

	/**main background color*/
	protected int color;

	/**Returns the color of the node*/
	public int getColor() {
		return color;
	}

	/**Sets the color relying on the type of node (selected, highlighted, and so on)*/
	public void setColor(int color, PApplet parent, StyleGraph style, PGraphics pg){
		if (color == Highligth)
			pg.fill(style.rHighlight, style.gHighlight, style.bHighlight);
		else if (color == Selected)
			pg.fill(style.rSelected, style.gSelected, style.bSelected);
		else
			pg.fill(style.rNonSelected, style.gNonSelected,style.bNonSelected);
	}

	/**
	 * Override this method to draw specific border line and shape types.
	 * <p>
	 * 
	 * @param node The node to draw
	 * @param parent The PApplet containing the map.
	 */
	public void draw(Node node, PApplet parent, PGraphics pg, StyleGraph style){
		//System.out.println("ancho="+node.width+" alto="+node.height);
		if(node.selected){
			pg.fill(style.rSelected,style.gSelected,style.bSelected);
		}else{
			pg.fill(style.rNonSelected, style.gNonSelected,style.bNonSelected);
		}
		pg.rectMode(PApplet.CORNERS);
		pg.rect(node.x1,node.y1,node.x2,node.y2);
	}
	
	/**Font used in the node's text*/
	private PFont font;
	/**Default height*/
	private float defHeigth;
	/**Current height. This is altered when zoom is activated*/
	private float heigth;

	private ArrayList<String> list;
	
	/**Emoticon code*/
	private int emoticon;
	/**Space object's code*/
	private int spaceObjects;
	
	/**
	 * Takes a node and assigns it a font different if it is root, leaf, ...
	 * If the text's size is bigger than the node width, it is split in different lines.
	 * 
	 * After that, the space for icons and space is calculated.
	 * 
	 * Calculate the size of each element is known as prepare the node.
	 * 
	 * @param node The node to be drawn
	 * @param style The Style used to paint.
	 * */
	public void prepareNode(Node node, StyleGraph style) {
		
		//Text
			defHeigth = style.defHeigthRoot;
			font = style.fontRoot;
		
		//Multi-Line text
		list = TextUtil.getSize(style,font,node.getInfo());
		heigth = node.height/(list.size()-1);
		
		//Vars of spaces
		int initSpace = (int)((heigth*defInitSpace)/defHeigth); // Rate 5 px, because font is normal
		//int initIcons =(int)((heigth*defSpaceIcons)/defHeigth);
		
		if(initSpace>defInitSpace) initSpace=defInitSpace;
		
		int contDist = initSpace;

		//Emoticon
		emoticon = (int)((heigth*defLongEmoticon)/defHeigth);
		spaceObjects =(int)((heigth*defSpaceObjects)/defHeigth);
		
		if(emoticon>defLongEmoticon) emoticon=defLongEmoticon;
		if(spaceObjects>defSpaceObjects) spaceObjects=defSpaceObjects;
		
		if(node.emoticones!=null){
			contDist = contDist + (emoticon+spaceObjects)*node.emoticones.size();
		}
	}
	
	/**Draws the content of a node: spaces and emoticons*/
	public void content(Node node, PApplet parent, StyleGraph style,PGraphics pg)
	{
		int i;
		//Variables of spaces
		int contDist = (int)((heigth*defInitSpace)/defHeigth); // RELACION 5 PIXELES CUANDO LA FUENTE ES NORMAL		
		String urlResources = "resources/flags/";
		
		if(node.emoticones!=null){
			if(emoticon<defLongEmoticon)
			{
				for(String dir : node.emoticones)
				{
					PImage newImage = parent.loadImage(urlResources + dir + ".png");
					parent.image(newImage, node.x1+contDist, node.y1+(node.height/2)-(emoticon/2),emoticon,emoticon);
					contDist=(int)(contDist+emoticon+spaceObjects); 
				}
			}else
			{
				for(String dir : node.emoticones)
				{
					PImage newImage = parent.loadImage(urlResources + dir + ".png");
					parent.image(newImage, node.x1+contDist, node.y1+(node.height/2)-(emoticon/2));
					contDist=contDist+emoticon+spaceObjects;
				}
			}
		}

		//create the font for the text
		font = parent.createFont("verdana",2);
		pg.fill(0);
		pg.textFont(font,node.fontSize);
		
		//Write the text line(s) of the node
		float heigthLocal = node.height/(list.size()-1);
		float heigthSet = heigthLocal/2;
		
		for(i=0;i<list.size()-1;i++)
		{
			pg.textMode(PApplet.SCREEN);
			pg.text(list.get(i), 
					node.x1+contDist, 
					node.y1 + heigthSet + (font.getFont().getSize()/2));

			heigthSet=heigthSet+heigthLocal;
		}
		//Draw the icons
		//icons(node,parent,style, pg);
	}
	
	public void content_auxiliar(Node node, PApplet parent, StyleGraph style,PGraphics pg,float K,int dx,int dy)
	{
		
		//Variables of spaces
		float heigth_auxiliar = K*heigth;
		float defInitSpace_auxiliar = K*defInitSpace;
		float defHeigth_auxiliar = K*defHeigth;
		
		//int contDist = (int)((heigth*defInitSpace)/defHeigth); // RELACION 5 PIXELES CUANDO LA FUENTE ES NORMAL
		int contDist = (int)((heigth_auxiliar*defInitSpace_auxiliar)/defHeigth_auxiliar);
		
		//String urlResources = "resources/flags/";
        /*
		if(node.emoticones!=null){
			if(emoticon<defLongEmoticon)
			{
				for(String dir : node.emoticones)
				{
					PImage newImage = parent.loadImage(urlResources + dir + ".png");
					parent.image(newImage, node.x1+contDist, node.y1+(node.height/2)-(emoticon/2),emoticon,emoticon);
					contDist=(int)(contDist+emoticon+spaceObjects); 
				}
			}else
			{
				for(String dir : node.emoticones)
				{
					PImage newImage = parent.loadImage(urlResources + dir + ".png");
					parent.image(newImage, node.x1+contDist, node.y1+(node.height/2)-(emoticon/2));
					contDist=contDist+emoticon+spaceObjects;
				}
			}
		}*/

		//Calculates the node's distance to place the text centered
		float node_heigth = node.height*K;
		float heigthLocal = node_heigth/(list.size()-1);
		
		//Para mayor seguridad podria calcularse el algoritmo de espacio.
		MindMapUtil temporal = new MindMapUtil();
		float heigthSet = heigthLocal/2;
		int node_x1 = PApplet.round(node.x1*K+dx);
		int node_y1 = PApplet.round(node.y1*K+dy);
		int node_x2 = PApplet.round(node.x2*K+dx);
		
		float fuente_size = temporal.textNoRecursiva(
				PApplet.abs(node_x1-node_x2),
				node.getInfo(),parent);
		pg.textMode(PApplet.SCREEN);
		font = parent.createFont("verdana",fuente_size);
		pg.fill(0);
		pg.textFont(font);
		pg.text(list.get(0), 
				node_x1+contDist, 
				node_y1 + heigthSet + (font.getFont().getSize()/2));
		//System.out.println("contDist="+contDist);
		//Draw the icons
		//icons(node,parent,style, pg);
	}

	
	/**Draws the icons of a node*/
	protected void icons(Node node, PApplet parent, StyleGraph style, PGraphics pg)
	{
		
		int initIcons =(int)((heigth*defSpaceIcons)/defHeigth);
		int widthIcon = (int)((heigth*def6Space)/defHeigth); //6 px as usual
		int heightIcons = (int)((heigth*def4Space)/defHeigth); //4 px as usual
	
		// If it has video, image, sketch or it is hidden...
		parent.fill(0);
		
		if (node.contImage != null) {
			pg.rect(node.x2 - initIcons, node.y1+heightIcons, widthIcon, heightIcons);
		}
		
		pg.fill(90);
		if (node.contVideo != null) {
			pg.rect(node.x2 - initIcons, node.y1+(node.height/2)-(heightIcons/2), widthIcon, heightIcons);
		}
		
		pg.fill(170);
		if (node.scrolled) {
			//System.out.println("entre al recuadro");
			pg.rect(node.x2 - initIcons, node.y2-heightIcons-heightIcons, widthIcon, heightIcons);
		}
	} 
}
